/*
 * Copyright (c) 2010-2020 IISI.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of IISI.
 */
package com.iisigroup.log.analysis.rdbms.dao;


import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 */
public class StuckDao extends TableJDBCDao{
    //================================================
    //== [Enumeration types] Block Start
    //== [Enumeration types] Block End 
    //================================================
    //== [static variables] Block Start
    private final static Logger LOGGER = LoggerFactory.getLogger(StuckDao.class);
    //== [static variables] Block Stop 
    //================================================
    //== [instance variables] Block Start
    //== [instance variables] Block Stop 
    //================================================
    //== [static Constructor] Block Start
    //== [static Constructor] Block Stop 
    //================================================
    //== [Constructors] Block Start (含init method)
    //== [Constructors] Block Stop 
    //================================================
    //== [Static Method] Block Start
    //== [Static Method] Block Stop 
    //================================================
    //== [Accessor] Block Start
    //== [Accessor] Block Stop 
    //================================================
    //== [Overrided Method] Block Start (Ex. toString/equals+hashCode)
    public void insertStuckEventData(final int initUid ,  final Map<String,Integer> map){
        final Connection conn = TableJDBCDao.getInstance();
        
        final String querySQL ="select * from stuck";
        final String sqlTemplate = "INSERT INTO STUCKEVENT (UID, HOST_IP , DATE , TIME , STACKTRACE_UID,CLIENT_IP,USER_ID ) VALUES (%s ,'%s' ,'%s','%s','%s','%s','%s' ) ;";
        final String sqlTemplateNull = "INSERT INTO STUCKEVENT (UID, HOST_IP , DATE , TIME , STACKTRACE_UID,CLIENT_IP,USER_ID) VALUES (%s ,'%s' ,'%s','%s','%s', null , null ) ;";
        Statement stmt;
        Statement stmt2;
        try {
            conn.setAutoCommit(false);
            stmt = conn.createStatement();
            stmt2= conn.createStatement();
            final ResultSet rs = stmt.executeQuery(querySQL);
            Integer uid = initUid+1;
            while (rs.next()) {
                // Retrieve by column name
                final String stacktrace = rs.getString("stacktrace"); 
                final Integer stacktraceUid = map.get(stacktrace);
                if(stacktraceUid == null ){
                    continue;
                }
                final String hostIp = rs.getString("HOST_IP"); 
                final String date = rs.getString("DATE"); 
                final String time = rs.getString("TIME"); 
                final String clientIp = rs.getString("CLIENT_IP");
                final String userId = rs.getString("USER_ID");
                if(clientIp==null || StringUtils.equalsIgnoreCase("null", clientIp)){                  
                   String sql =   String.format(sqlTemplateNull,  (uid++).toString(),hostIp,date, time ,stacktraceUid);
                   stmt2.executeUpdate(sql);
                   
                }else{
                    String sql =  String.format(sqlTemplate,(uid++).toString(),hostIp,date, time ,stacktraceUid ,clientIp ,userId);
                    stmt2.executeUpdate(sql);
                    
                }
            }
            conn.commit();
            rs.close();
            stmt.close();
            stmt2.close();
        } catch (SQLException e) {
             LOGGER.error(e.getMessage() , e) ;
             if(conn!=null){
                 try {
                    conn.rollback();
                } catch (SQLException e1) {
                    // TODO Auto-generated catch block
                   LOGGER.error("", e1);
                }
             }
        } finally {
            closeConnection(conn);
        }
    } 
    //== [Overrided Method] Block Stop 
    //================================================
    //== [Method] Block Start
    //####################################################################
    //## [Method] sub-block : 
    //####################################################################    
    //== [Method] Block Stop 
    //================================================
    //== [Inner Class] Block Start
    //== [Inner Class] Block Stop 
    //================================================
}
